﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.GamerServices; // för pause skylt
using Microsoft.Xna.Framework.Storage;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Audio;

namespace Group3Game
{
    class Player
    {
        private const int JUMP_MAX_HEIGHT = 150; //Maxhöjden vid vanligt hopp
        private const int DOUBLEJUMP_ALLOWED = 30; //Höjden vid vilken ett dubbelhopp är tillåtet
        private const int DOUBLEJUMP_MAX_HEIGHT = 300; //Maxhöjden vid dubbelhopp
        private const int START_POSITION_X = 150;
        private const int START_POSITION_Y = 350;
        private int JUMP_SPEED = 250;
        private int MOVE_UP = -2;
        private int MOVE_DOWN = 2;
        
        private int lives;
        private int score;

        public int Score
        {
            get { return score;  }
            set { score = value; }
        }

        public Vector2 Position;

        // Antal spelarliv
        public int Lives
        {
            get { return lives; }
            set { lives = value; }
        }

        
        Texture2D playerTexture;

        int FrameWidth;
        int FrameHeight;
        int elapsedTime;

        // State of the player
        //public bool Active;

        // Get the width of the player
        public int Width
        {
            get { return FrameWidth; }
        }

        // Get the height of the player
        public int Height
        {
            get { return FrameHeight; }
        }
        
        public enum PlayerState
        {
            Active,
            Jump,
            DoubleJump,
            Jumping,
            GameOver
        }

        SoundEffect jumpSound;
        SoundEffectInstance jumpSoundInstance;

        public static PlayerState State;
        private Vector2 playerDirection = Vector2.Zero;
        private Vector2 playerSpeed = Vector2.Zero;
        private Vector2 playerStartingPosition = Vector2.Zero;
        private KeyboardState previousKeyboardState;

        // Initialize the player
        public void Initialize(Texture2D texture, Vector2 position, int width, int height)
        {
            // Set the starting position of the player around the middle of the screen and to the back
            Position = position;

  
            // Set the player to be active
            State = PlayerState.Active;

            //Active = true;
            
            //Test:
            //State = PlayerState.Active;

            // Set the player health
            Lives = 3;


            Score = 0;

            // Katten
            playerTexture = texture;

            this.FrameWidth = width;
            this.FrameHeight = height;
            elapsedTime = 0;

            jumpSound = Game1.MyGame1.Content.Load<SoundEffect>("hopp1");
            jumpSoundInstance = jumpSound.CreateInstance();
        
        }

        public void Update(GameTime gameTime)
        {
            //if (Active == false)
                //return;

            //if (State != PlayerState.Active)
                //return;

            KeyboardState currentKeyboardState = Keyboard.GetState();
            UpdateJump(currentKeyboardState, gameTime);
            previousKeyboardState = currentKeyboardState;
            
            elapsedTime += (int)gameTime.ElapsedGameTime.TotalMilliseconds;
            Position += playerDirection * playerSpeed * (float)gameTime.ElapsedGameTime.TotalSeconds;

            TimeSpan tid = gameTime.TotalGameTime;
        }
        public void Draw(SpriteBatch spriteBatch)
        {
            //PlayerAnimation.Draw(spriteBatch);
            //base.Draw(spriteBatch);
            spriteBatch.Draw(playerTexture, Position, Color.White);
        }

        public void UpdateJump(KeyboardState currentKeyboardState, GameTime gameTime)
        {
            if (State == PlayerState.Active) {
                if (currentKeyboardState.IsKeyDown(Keys.Space) == true) {
                    jumpSound.Play();
                    Jump();
                }
            }

            if (State == PlayerState.Jumping) {

                //Kollar om spelaren trycker på space igen och om katten nått en tillåten höjd för att göra nästa hopp,
                //denna höjd kan lätt ändras genom att ge konstanten DOUBLEJUMP_ALLOWED ibland attributen. Denna används för
                //att är den inte med hoppar katten direkt dubbelt så högt p.g.a. att spelet tro annars att den första knapptryckningen
                //är denna knapptryckningen och katten hoppar dubbelt så högt utan att vi egentligen vill att den skall det.
                if (currentKeyboardState.IsKeyDown(Keys.Space) == true && previousKeyboardState.IsKeyDown(Keys.Space) == false && (playerStartingPosition.Y - Position.Y) >= DOUBLEJUMP_ALLOWED) {
                    //Lagt till ett tillstånd för att göra koden mindre rörig
                    State = PlayerState.DoubleJump;
                }

                if (playerStartingPosition.Y - Position.Y > JUMP_MAX_HEIGHT)
                    playerDirection.Y = MOVE_DOWN;

                if (Position.Y > playerStartingPosition.Y) {
                    Position.Y = playerStartingPosition.Y;
                    State = PlayerState.Active;
                    playerDirection = Vector2.Zero;
                }
            }

            //Är vi i det nya tillståndet skall vi hoppa högre. Den nya höjden ges av DOUBLEJUMP_MAX_HEIGHT
            //som är en konstant som kan ändras längst upp ibland attributen. utöver den nya höjden beter sig
            //karaktären precis som vid ett vanligt hopp.
            if (State == PlayerState.DoubleJump) {
                if (playerStartingPosition.Y - Position.Y > DOUBLEJUMP_MAX_HEIGHT)
                    playerDirection.Y = MOVE_DOWN;

                if (Position.Y > playerStartingPosition.Y) {
                    Position.Y = playerStartingPosition.Y;
                    State = PlayerState.Active;
                    playerDirection = Vector2.Zero;
                }
            }
        }

        public void Jump()
        {
            if (State != PlayerState.Jumping) {
                State = PlayerState.Jumping;
                playerStartingPosition = Position;
                playerDirection.Y = MOVE_UP;
                playerSpeed = new Vector2(JUMP_SPEED, JUMP_SPEED);
            }
        }
    }
}
